package com.atguigu.service.impl;

import com.atguigu.dao.UserMapper;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAll() {
        BoundValueOperations boundValueOperations = redisTemplate.boundValueOps("userList");
        Object userListObj = boundValueOperations.get();
        List<User> userList = null ;
        if(userListObj==null){
            System.out.println("从数据库查询...");
            userList = userMapper.selectAll();
            System.out.println("保存到redis...");
            boundValueOperations.set(userList);
        }else{
            System.out.println("从redis获取...");
            userList = (List<User>)userListObj ;
        }
        return userList ;
    }
}
